{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"gfedcba\""
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let mut s = \"abcdefg\";\n",
    "let s_rev:String = s.chars().rev().collect();\n",
    "s_rev\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "olleh\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{\n",
    "    let s = String::from(\"hello\");\n",
    "    let reversed: String = s.chars().rev().collect();\n",
    "    println!(\"{}\", reversed);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ConfigRaw([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#[derive(Debug)]\n",
    "struct ConfigRaw([u8; 5]);\n",
    "\n",
    "let c = ConfigRaw([1, 2, 3, 4, 5]);\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# bitfield"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "Couldn't automatically determine type of variable `c`.\nPlease give it an explicit type.",
     "output_type": "error",
     "traceback": [
      "Couldn't automatically determine type of variable `c`.\nPlease give it an explicit type."
     ]
    }
   ],
   "source": [
    ":dep modular-bitfield=\"0.11\"\n",
    "\n",
    "use modular_bitfield::prelude::*;\n",
    "\n",
    "\n",
    "// #[derive(Default, Debug)]\n",
    "// #[bitfield(filled = false)]\n",
    "// pub struct Config{\n",
    "//     parity: B1,\n",
    "//     pcbattna: B3,\n",
    "//     pcbdac20a: B9,\n",
    "//     pcbdac10a: B9,\n",
    "// }\n",
    "#[bitfield(filled = false)]\n",
    "pub struct SomeBitsUndefined {\n",
    "    is_compact: bool,\n",
    "    is_secure: bool,\n",
    "    pre_status: B3,\n",
    "}\n",
    "\n",
    "\n",
    "let c = SomeBitsUndefined::new();\n",
    "println!(\"{:?}\", c.into_bytes());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "#[macro_export]\n",
    "macro_rules! myvec {\n",
    "    ($($x:expr),*) => {\n",
    "        {\n",
    "            let mut temp_vec = Vec::new();\n",
    "            $(\n",
    "                temp_vec.push($x);\n",
    "            )*\n",
    "            $(\n",
    "                temp_vec.push($x);\n",
    "            )*\n",
    "            temp_vec\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "let v = myvec![1, 2, 3 ];\n",
    "println!(\"{:?}\", v);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个数字 (0-255):\n",
      "实例化的Foo变量: Foo { t: 1, b: 3, c: 3 }\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ":dep bitfield-struct = \"0.9\"\n",
    "use bitfield_struct::bitfield;\n",
    "use std::io;\n",
    "\n",
    "\n",
    "#[bitfield(u8)]\n",
    "#[derive(PartialEq, Eq)]\n",
    "struct Foo {\n",
    "    #[bits(1)]\n",
    "    t: u8,\n",
    "    #[bits(2)]\n",
    "    b: u8,\n",
    "    #[bits(2)]\n",
    "    c: u8,\n",
    "    #[bits(3)]\n",
    "    __: u8,\n",
    "}\n",
    "\n",
    "\n",
    "{\n",
    "    // 提示用户输入一个数字\n",
    "    println!(\"请输入一个数字 (0-255):\");\n",
    "\n",
    "    // 创建一个可变字符串来存储用户输入\n",
    "    let mut input = String::from(\"127\");\n",
    "\n",
    "    // // 读取用户输入\n",
    "    // io::stdin()\n",
    "    //     .read_line(&mut input)\n",
    "    //     .expect(\"读取输入失败\");\n",
    "\n",
    "    // 将输入的字符串转换为u8类型\n",
    "    let num: u8 = match input.trim().parse() {\n",
    "        Ok(n) => n,\n",
    "        Err(_) => {\n",
    "            println!(\"请输入一个有效的数字\");\n",
    "            return;\n",
    "        }\n",
    "    };\n",
    "\n",
    "    // 使用用户输入的数字实例化Foo结构体\n",
    "    let foo = Foo::from_bits(num);\n",
    "\n",
    "    // 打印实例化后的Foo变量\n",
    "    println!(\"实例化的Foo变量: {:?}\", foo);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b11111011\n"
     ]
    }
   ],
   "source": [
    "let x:i8 = -10;\n",
    "\n",
    "println!(\"0b{:b}\", (x<<3)>>4);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b11111111\n"
     ]
    }
   ],
   "source": [
    "let x:u8 = 0b11111;\n",
    "\n",
    "println!(\"0b{:0b}\", ((x<<3) as i8)>>3);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "refutable pattern in local binding",
     "output_type": "error",
     "traceback": [
      "\u001b[31m[E0005] Error:\u001b[0m refutable pattern in local binding",
      "    \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_24:1:1\u001b[38;5;246m]\u001b[0m",
      "    \u001b[38;5;246m│\u001b[0m",
      " \u001b[38;5;246m22 │\u001b[0m \u001b[38;5;249ml\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54mV\u001b[0m\u001b[38;5;54ma\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54mu\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mT\u001b[0m\u001b[38;5;54my\u001b[0m\u001b[38;5;54mp\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54mI\u001b[0m\u001b[38;5;54m8\u001b[0m\u001b[38;5;54m(\u001b[0m\u001b[38;5;54my\u001b[0m\u001b[38;5;54m)\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m=\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249mV\u001b[0m\u001b[38;5;249ma\u001b[0m\u001b[38;5;249ml\u001b[0m\u001b[38;5;249mu\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249mT\u001b[0m\u001b[38;5;249my\u001b[0m\u001b[38;5;249mp\u001b[0m\u001b[38;5;249me\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249m:\u001b[0m\u001b[38;5;249mU\u001b[0m\u001b[38;5;249m8\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249mx\u001b[0m\u001b[38;5;249m<\u001b[0m\u001b[38;5;249m<\u001b[0m\u001b[38;5;249m3\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m.\u001b[0m\u001b[38;5;249mi\u001b[0m\u001b[38;5;249mn\u001b[0m\u001b[38;5;249mt\u001b[0m\u001b[38;5;249mo\u001b[0m\u001b[38;5;249m(\u001b[0m\u001b[38;5;249m)\u001b[0m\u001b[38;5;249m;\u001b[0m",
      " \u001b[38;5;240m   │\u001b[0m     \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m                             \u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m   │\u001b[0m             \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m patterns `ValueType::U8(_)`, `ValueType::U16(_)`, `ValueType::U32(_)` and 7 more not covered",
      " \u001b[38;5;240m   │\u001b[0m                                                  \u001b[38;5;100m│\u001b[0m ",
      " \u001b[38;5;240m   │\u001b[0m                                                  \u001b[38;5;100m╰\u001b[0m\u001b[38;5;100m─\u001b[0m help: you might want to use `let else` to handle the variants that aren't matched: ` else { todo!() }`",
      "\u001b[38;5;246m────╯\u001b[0m"
     ]
    }
   ],
   "source": [
    "enum ValueType {\n",
    "    U8(u8),\n",
    "    U16(u16),\n",
    "    U32(u32),\n",
    "    U128(u128),\n",
    "    I8(i8),\n",
    "    I16(i16),\n",
    "    I32(i32),\n",
    "    I128(i128),\n",
    "    Bool(bool),\n",
    "    Bin(String),\n",
    "    Bytes(Vec<u8>),\n",
    "}\n",
    "\n",
    "\n",
    "let x:u8 = 0b11111;\n",
    "\n",
    "// fn convert<T>(x: u8, n: u8) -> T\n",
    "// where T: From<u8> {\n",
    "//     (x << n) as T\n",
    "// }\n",
    "let ValueType::I8(y) = ValueType::U8(x<<3).into();\n",
    "\n",
    "println!(\"0b{:0b}\", y>>3);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "63"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u8::MAX>>2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "255"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "let x = (-1i8 as u8);\n",
    "x"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Rust",
   "language": "rust",
   "name": "rust"
  },
  "language_info": {
   "codemirror_mode": "rust",
   "file_extension": ".rs",
   "mimetype": "text/rust",
   "name": "Rust",
   "pygment_lexer": "rust",
   "version": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
